def mul(pre,p):
    cur = []
    for i in range(3):
        res = pre[0]*p[i][0] + pre[1]*p[i][1] + pre[2]*p[i][2]
        cur.append(res)
    return cur

def poss(step,cur,p):
    for i in range(step):
        cur = mul(cur,p)
    if cur[2]>0.5:
        return 1
    else:
        return 0

T = int(input())
for _ in range(T):
    step = int(input())
    cur = list(map(float,input().split()))
    print(cur)
    p = []
    for _ in range(3):
        p.append(list(map(float,input().split())))
    res = poss(step,cur,p)
    print(res)
